<template>
    <el-form :model="user" label-position="right" label-width="auto">
        <el-form-item label="用户名" prop="yhm">
            <el-input v-model="user.yhm" placeholder="用户名" disabled clearable/>
        </el-form-item>
        <el-form-item label="用户昵称" prop="yhnc">
            <el-input v-model="user.yhnc" placeholder="用户昵称" clearable/>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
            <el-radio-group v-model="user.sex">
                <el-radio value="男">男</el-radio>
                <el-radio value="女">女</el-radio>
            </el-radio-group>
        </el-form-item>
        <el-form-item label="用户身份" prop="yhsf">
            <el-select v-model="user.yhsf" placeholder="请选择" clearable>
                <el-option label="管理员" value="0"/>
                <el-option label="普通管理员" value="2"/>
                <el-option label="用户" value="3"/>
            </el-select>
        </el-form-item>
        <el-form-item label="手机号" prop="sjh">
            <el-input v-model="user.sjh" placeholder="手机号" clearable/>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
            <el-input v-model="user.email" placeholder="邮箱" clearable/>
        </el-form-item>
    </el-form>
    <el-row>
        <el-col>
            <div style="text-align: center;">
                <el-button type="primary" @click="doSave">保存</el-button>
                <el-button @click="doCancel">取消</el-button>
            </div>
        </el-col>
    </el-row>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import {ElMessage} from "element-plus";
import axios from 'axios';

//---------------------------- 自定义组件事件 -----------------------------
const emits = defineEmits([
    "closeDialog",	// 关闭 dialog
    "flushPage"		  // 刷新页面
]);

const user = ref({
    yhbh: '',
    yhm: '',
    pwd: '',
    yhnc: '',
    yhsf: '',
    sex: '',
    sjh: '',
    email: '',
})

// 将父组件传递来的的参数，保存到 user 中
const handleUpdateData = (obj) => {
    user.value = obj;
}

// 暴露给父组件使用的方法
defineExpose({
    handleUpdateData
})

// 取消
const doCancel = () => {
    emits("closeDialog");
}

// 更新
const doSave = async() => {
    const {data} = await axios.put("/api/user",user.value);
    if (data > 0) {
        emits("flushPage");				// 触发事件：刷新页面
        emits("closeDialog");			// 触发事件：关闭 更新 dialog
        ElMessage.success("操作成功！");
    } else {
        ElMessage.error("操作失败！");
    }
}
</script>